﻿using System;
using System.Data;
using System.Windows.Forms;
using TestSystem.DataManipulation;
using TestSystem.DataManipulation.Helpers;
using TestSystem.Ui.Controls;

namespace TestSystem.Ui
{
    public partial class MainForm
    {
        
        private void CreateBarStyle()
        {
            DataGrid.Columns[0].Visible = false;
            DataGrid.Columns[1].HeaderText = @"DateTime";
            DataGrid.Columns[1].DefaultCellStyle.Format =  @"MM.dd.yyyy HH:mm:ss";
            DataGrid.Columns[2].HeaderText = @"Open";
            DataGrid.Columns[3].HeaderText = @"High";
            DataGrid.Columns[4].HeaderText = @"Low";
            DataGrid.Columns[5].HeaderText = @"Close";
            DataGrid.Columns[6].HeaderText = @"Volume";

        }
        private void CreateTickStyle()
        {
            DataGrid.Columns[0].Visible = false;
            DataGrid.Columns[1].HeaderText = @"DateTime";
            DataGrid.Columns[1].DefaultCellStyle.Format = @"MM.dd.yyyy HH:mm:ss";
            DataGrid.Columns[2].HeaderText = @"Price";
            DataGrid.Columns[3].HeaderText = @"Volume";
        }
        private void ShowCurrentData()
        {
            if (!(treeViewData.SelectedNode is FrequencyTreeNode)) return;
            var quotetype = GetQuoteType();
            
            var toolid = (Int32)treeViewData.SelectedNode.Parent.Tag;
            switch (quotetype)
            {
                case (QuoteType.Bar):
                    {
                        var currentreader = SqlHelper.ExecuteAndReturnDataReader(@"GetCurrentToolBars",
                                                                                 new Param(@"toolid", SqlDbType.Int, toolid), new Param(@"FreqType", SqlDbType.Int, treeViewData.SelectedNode.Tag));
                        
                        if ((!currentreader.HasRows))
                        {
                            currentreader.Close();
                            currentreader.Dispose();
                            DataGrid.Columns.Clear();
                            return;
                        }
                        var bs = new BindingSource();
                        bs.DataSource = currentreader;
                        DataGrid.Columns.Clear();
                        DataGrid.DataSource = bs;
                        CreateBarStyle();
                        currentreader.Close();
                        currentreader.Dispose();
                        break;
                    }

                case (QuoteType.Tick):
                    {

                        var currentreader = SqlHelper.ExecuteAndReturnDataReader(@"GetCurrentToolTicks",
                                                                                 new Param(@"toolid", SqlDbType.Int, toolid), new Param(@"FreqType", SqlDbType.Int, treeViewData.SelectedNode.Tag));
                        if ((!currentreader.HasRows))
                        {
                            currentreader.Close();
                            currentreader.Dispose();
                            DataGrid.Columns.Clear();
                            return;
                        }
                        
                        var bs = new BindingSource();
                        bs.DataSource = currentreader;
                        DataGrid.Columns.Clear();
                        DataGrid.DataSource = bs;
                        CreateTickStyle();
                        currentreader.Close();
                        currentreader.Dispose();
                        break;
                    }
            }
        }
    }
}
